package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.core.framework.dao.mybatis.paginator.domain.PageBounds;
import com.jiyinit.web.core.business.dto.TreePageRequest;
import com.jiyinit.web.core.business.entity.TabCoreResourceEntity;
import com.jiyinit.web.core.business.vo.ResourceVo;
import com.jiyinit.web.core.business.vo.RoleResourceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lishaochuan
 * @version 1.0
 * @date 2017/4/11 16:14
 * @since 1.0
 */
@Repository("core.tabCoreOperateLogDao")
public class TabCoreResourceDao {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(TabCoreResourceDao.class);

    private String SQLID = "core.tabCoreResourceDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;


    /**
     * 添加资源树
     * @author lishaochuan
     * @create 2017/4/18 10:05
     * @param
     * @return
     */
    public int save(TabCoreResourceEntity tabCoreResource) {
        return mybatisDaoContext.save(SQLID + "save", tabCoreResource);
    }

    /**
     * 修改资源树
     * @author lishaochuan
     * @create 2017/4/19 15:56
     * @param
     * @return
     */
    public int update(TabCoreResourceEntity tabCoreResource) {
        return mybatisDaoContext.update(SQLID + "update", tabCoreResource);
    }

    /**
     * 查询超级管理员权限树
     * @return
     */
    public List<ResourceVo> findAllResource(Integer userId){
        return mybatisDaoContext.findAll(SQLID+"findAllResource", ResourceVo.class, userId);
    }

    /**
     * 根据用户ID获取用户权限树
     * @author lishaochuan
     * @create 2017/4/12 9:02
     * @param
     * @return
     */
    public List<ResourceVo> findResourceByUser(Integer userId){
        return mybatisDaoContext.findAll(SQLID+"findResourceByUser", ResourceVo.class, userId);
    }


    /**
     * 查询角色权限树
     * @author lishaochuan
     * @create 2017/4/20 17:55
     * @param
     * @return
     */
    public List<RoleResourceVo> findResourceByRoleId(Integer roleId){
        return mybatisDaoContext.findAll(SQLID+"findResourceByRoleId", RoleResourceVo.class, roleId);
    }


    /**
     * 根据id查询菜单
     * @author lishaochuan
     * @create 2017/4/17 18:21
     * @param
     * @return
     */
    public TabCoreResourceEntity selectById(Integer parentId){
        return mybatisDaoContext.findOne(SQLID+"selectById", TabCoreResourceEntity.class, parentId);
    }


    /**
     * 根据parentId查询子节点树
     * @author lishaochuan
     * @create 2017/4/17 11:48
     * @param
     * @return
     */
    public List<TabCoreResourceEntity> selectByParentId(Integer parentId){
        return mybatisDaoContext.findAll(SQLID+"selectByParentId", TabCoreResourceEntity.class, parentId);
    }

    /**
     * 分页根据parentId查询子节点树
     * @author lishaochuan
     * @create 2017/4/17 11:48
     * @param
     * @return
     */
    public PagingResult<TabCoreResourceEntity> selectPageByParentId(TreePageRequest treePageRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(treePageRequest.getLimit());
        pageBounds.setPage(treePageRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "selectByParentId", TabCoreResourceEntity.class, treePageRequest.toMap(), pageBounds);
    }

}
